<template>
    <el-row :gutter="20" style="padding:20px 10px;margin:0px">
        <el-col :span="24">
            <el-form ref="form" :model="form" label-width="80px" label-position="left" size="mini">
                <el-form-item label="客户姓名">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="客户性别">
                    <el-radio-group v-model="form.sex">
                        <el-radio :label="1">男</el-radio>
                        <el-radio :label="0">女</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="客户电话">
                    <el-input v-model="form.phone"></el-input>
                </el-form-item>
                <el-form-item label="会员卡号">
                    <el-input v-model="form.vipcard"></el-input>
                </el-form-item>
                <el-form-item label="客户类别" v-show="!form.id">
                    <el-radio-group v-model="form.initial_info">
                        <el-radio :label="1">新客</el-radio>
                        <el-radio :label="0">老客</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="客户描述">
                    <el-input type="textarea" v-model="form.remarks"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submit">保存</el-button>
                    <el-button @click="close">取消</el-button>
                </el-form-item>
            </el-form>
        </el-col>
    </el-row>
</template>
<script>
import C from '@/common/common.js'
import Guest from '@/service/shop/guest.js'
export default {
  name: 'addGuest',
  data () {
    return {
      form: {
        id: 0,
        name: '',
        phone: '',
        sex: 0,
        vipcard: '',
        initial_info: 1,
        remarks: ''
      }
    }
  },
  methods: {
    async submit () {
      if (!(/^[\u4e00-\u9fa5]{2,4}$/.test(this.form.name))) {
        C.error('姓名格式不正确，请重新输入')
        return false
      } else if (!(/^1[345678]\d{9}$/.test(this.form.phone))) {
        C.error('手机号格式不正确，请重新输入')
        return false
      } else if (this.form.vipcard == '') {
        C.error('会员卡号不能为空，请重新输入')
        return false
      }
      let data = await Guest.addGuest(this.form)
      if (data) C.closeBrowserWindow(data)
    },
    close () {
      C.closeBrowserWindow()
    }
  },
  mounted: function () {
    let row = C.getDatas()
    if (row.id) {
      this.form.id = row.id
      this.form.name = row.name
      this.form.phone = row.phone
      this.form.sex = row.sex
      this.form.vipcard = row.vipcard
      this.form.initial_info = row.initial_info
      this.form.remarks = row.remarks
    }
  }
}
</script>